package io.lkt.modules.sys.controller;

import io.lkt.common.util.Query;
import io.lkt.common.utils.PageUtils;
import io.lkt.common.utils.R;
import io.lkt.modules.sys.entity.BBrandShieldEntity;
import io.lkt.modules.sys.service.BBrandShieldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
import java.util.Objects;


/**
 * 品牌(换成我的)屏蔽表
 *
 * @author xss
 * @email dustwas@163.com
 * @date 2021-09-02 09:19:51
 */
@RestController
@RequestMapping("/sys/bbrandshield")
public class BBrandShieldController {
    @Autowired
    private BBrandShieldService bBrandShieldService;

    @RequestMapping("/getList")
    public R getList(@RequestParam Map<String, Object> params) {
        Query query = new Query(params);
        List<BBrandShieldEntity> list = bBrandShieldService.getList(query);
        int total = bBrandShieldService.getListTotal(query).intValue();

        PageUtils page = new PageUtils(list, total, query.getLimit(), query.getPage());
        return R.ok().put("page", page);
    }

    /**
     * 保存品牌屏蔽信息
     * @param entity 请求实体
     * @return
     */
    @RequestMapping("/save")
    public R save(@RequestBody BBrandShieldEntity entity) {
        if (Objects.isNull(entity) || Objects.isNull(entity.getBrandId()) || Objects.isNull(entity.getGzhCode())) {
            return R.error("参数异常");
        }
        BBrandShieldEntity bBrandShieldEntity = bBrandShieldService.queryByBrand(entity.getBrandId(), entity.getGzhCode());
        if (Objects.isNull(bBrandShieldEntity)) {
            bBrandShieldService.saveObject(entity);
        } else {
            return R.error("您选择的公众号已屏蔽该品牌");
        }
        return R.ok();
    }

    /**
     * 删除品牌屏蔽信息
     * @param id 主键ID
     * @return
     */
    @RequestMapping("/del/{id}")
    public R del(@PathVariable("id") Integer id) {
        if (Objects.isNull(id) || id.intValue() <= 0 ) {
            return R.error("参数异常");
        }
        bBrandShieldService.deleteById(id);
        return R.ok();
    }

}
